<?php
namespace app\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Department;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class DepartmentController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }

    /**
     * 获取部门列表，仅供 zTree 使用
     */
    public function getZtreeRows()
    {
        $raw = Department::select('id', 'f_id', 'name');
        $rows = $raw->orderBy('id', 'ASC')->get();
        // $rows->prepend(collect(['id' => 'undefined', 'f_id' => 0, 'name' => '未定义']));
        return $this->response($rows);
    }

    /**
     * 获取部门用户列表
     */
    public function getUserRows(Request $request)
    {
        $id = intval($request->input('id'));
        $rows = User::select('id', 'username', 'realname')->where('dept_id', $id)->orderBy('id')->get();
        $data['rows'] = $rows;
        return $this->response($data);
    }

    /**
     * 新建部门
     */
    public function postCreate(Request $request)
    {
        // 验证必填字段非空
        $validator = Validator::make($request->all(), [
            'name' => 'required|max:50',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->messages()->first());
        }

        $f_id = intval($request->input('f_id'));
        $name = safe($request->input('name'), 50);

        if (!empty($f_id)) {
            $row = Department::find($f_id);
            if (empty($row)) {
                return $this->error('上级部门不存在');
            }
        }

        // 检查重复
        $n = Department::where(['f_id' => $f_id, 'name' => $name])->count('id');
        if ($n > 0) {
            return $this->error('部门名称重复');
        }

        $dept = new Department;
        $dept->f_id = $f_id;
        $dept->name = $name;
        $dept->save();
        return $this->response($dept);
    }

    /**
     * 编辑部门名称
     */
    public function postRename(Request $request)
    {
        //验证必填字段非空
        $validator = Validator::make($request->all(), [
            'id' => 'required',
            'f_id' => 'required',
            'name' => 'required|max:50',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->messages()->first());
        }

        //查重
        $dep = Department::where('name', $request->name)->where('f_id', $request->f_id)->first();
        if (!empty($dep)) {
            return $this->error('该子级部门已存在');
        }

        //更新
        $dep = Department::find($request->id);
        if (empty($dep)) {
            return $this->error('您要编辑的部门不存在');
        }
        $dep->name = $request->name;
        $dep->save();

        return $this->response($dep);
    }

    /**
     * 删除部门
     */
    public function postDelete(Request $request)
    {
        $id = intval($request->input('id'));
        $dept = Department::find($id);
        if (empty($dept)) {
            return $this->error('您要删除的部门不存在');
        }

        //是否有子部门
        $n = Department::where('f_id', $id)->count('id');
        if ($n > 0) {
            return $this->error('禁止删除有下级部门的部门');
        }

        //是否有人员
        $n = User::where('dept_id', $id)->count('id');
        if ($n > 0) {
            return $this->error('请先删除其下属的用户');
        }

        //删除
        $dept->delete();
        return $this->response($id);
    }
}
